package com.star.common.utils;

import com.star.common.base.BaseEnum;
import com.star.common.vo.DictVO;
import org.reflections.Reflections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 字典工具类型
 *
 * @author yangzj
 */
public class DictEnums {

    /**
     * 获取所有实现了BaseEnum的枚举
     *
     * @return {@link List}<{@link DictVO}>
     */
    public static List<DictVO> getEnums() {
        List<DictVO> enumDicts = new ArrayList<>();
        Reflections reflections = new Reflections("com.star");
        Set<Class<? extends BaseEnum>> subTypesOf = reflections.getSubTypesOf(BaseEnum.class);
        subTypesOf.forEach(c -> {
            AtomicInteger sort = new AtomicInteger(1);
            enumDicts.addAll(Arrays.stream(c.getEnumConstants())
                    .map(b -> new DictVO()
                            .setTypeCode(b.getTypeCode()).setTypeName(b.getTypeName())
                            .setName(b.getName()).setCode(b.getCode().toString())
                            .setExpand(b.getExpand())
                            .setSort(sort.getAndIncrement()).setIsEdit(false))
                    .collect(Collectors.toList()));
        });
        return enumDicts;
    }


}
